﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Ccit.Utils;
using System.Collections.Generic;
using Ccit.OA.Utils;
using Ccit.Mgoa.MsPsnset;

namespace Ccit.Mgoa.Collaboration
{
    public partial class collaboration_pesonal_edit : Ccit.Utils.Page
    {
        LoginUser logUser = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                logUser = (LoginUser)Session["LogUserInfo"];
                string sId = (Request.QueryString["id"] != null) ? Request.QueryString["id"].ToString() : "";
                hdId.Value = sId;
                InitDDL();
                InitPage();
            }
        }

        private void InitPage()
        {
            string sId=hdId.Value;
            hdPublisherId.Value = logUser.UserId;
            tdPulisher.InnerText = logUser.UserName;
            tdPublishOrg.InnerText = logUser.OrganName + "/" + logUser.DeptName;
            string sCurDate = DateTime.Now.ToString("yyyy-MM-dd");
            txtPublishDate.Value = sCurDate;
            txtFinishDate.Value = sCurDate;
            tdCreateDate.InnerText = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            CollaborateBLL oCollaborate = new CollaborateBLL();
            if (sId != "")
            {
                trRemind.Visible = false;
                MsCollaborativeTopic message = oCollaborate.GetCollaborativeTopicById(sId);
                txtTitle.Value = message.Title;
                txtDesc.Text = message.Content;
                selCollaborativeType.Value = message.TypeId;
                hdPublisherId.Value = message.PublisherId;
                txtPublishDate.Value = message.PublishDate;
                txtFinishDate.Value = message.FinishDate;
                hdPublishRangeType.Value = message.RangeType;
                hdPublishRange.Value = message.RangeId;
                hdStatus.Value = message.Status;
                txtPublishRange.Text = SysInterface.GetNameStrByIdStr(message.RangeId, message.RangeType, "");
                attUpload.EditShow("0040", sId);
            }
            else
            {
                MsRemind msRemind = oCollaborate.GetRemindSetByTypeCode("a4");
                if (msRemind.mesflag == "0" && msRemind.telflag == "0")
                {
                    trRemind.Style["display"] = "none";
                    radNo.Checked = true;
                    radNo1.Checked = true;
                }
                else if (msRemind.mesflag == "1" && msRemind.telflag == "1")
                {
                    tdNbsp.Style["display"] = "none";
                    radYes.Checked = true;
                    radYes1.Checked = true;
                }
                else
                {
                    if (msRemind.telflag == "1")
                    {
                        tdMsgTitle.Style["display"] = "none";
                        tdMsgRemind.Style["display"] = "none";
                        radYes1.Checked = true;
                        radNo.Checked = true;
                    }
                    else if (msRemind.mesflag == "1")
                    {
                        tdPhoneTitle.Style["display"] = "none";
                        tdPhoneRemind.Style["display"] = "none";
                        radYes.Checked = true;
                        radNo1.Checked = true;
                    }
                }
            }
        }

        private void InitDDL()
        {
            CollaborateBLL oCollaborate=new CollaborateBLL();
            List<MsCollaborativeType> lstCollaborativeType = oCollaborate.GetPowerRangeCollaborativeType("2");
            List<MsCollaborativeType> lstManageType = oCollaborate.GetPowerRangeCollaborativeType("1");
            MyCustomCompare myCompare = new MyCustomCompare();
            List<MsCollaborativeType> lstResult = lstCollaborativeType.Concat(lstManageType).ToList();
            List<MsCollaborativeType> lstResult1 = lstResult.Distinct(myCompare).ToList();
            selCollaborativeType.DataSource = lstResult1;
            selCollaborativeType.DataTextField = "TypeName";
            selCollaborativeType.DataValueField = "TypeId";
            selCollaborativeType.DataBind();
        }

        protected void lnkSave_Click(object sender, EventArgs e)
        {
            CollaborateBLL oCollaborate=new CollaborateBLL();
            MsCollaborativeTopic message = new MsCollaborativeTopic();
            string sId = hdId.Value;
            string sTitle = txtTitle.Value.Trim();
            string sContent = txtDesc.Text.Trim();
            string sTypeId = selCollaborativeType.Value;
            string sPublisherId = hdPublisherId.Value ;
            string sPublishDate = txtPublishDate.Value.Trim();
            string sFinishDate = txtFinishDate.Value.Trim();
            string sRangeType = hdPublishRangeType.Value;
            string sRangeId = hdPublishRange.Value;
            string sStatus = hdStatus.Value;
            bool bIsMsgRemind = false;
            bool bIsPhoneRemind = false;
            if (sId == "")
            {
                if (radYes.Checked)
                    bIsMsgRemind = true;
                if (radYes1.Checked)
                    bIsPhoneRemind = true;
            }
            message.Id = sId;
            message.Title = sTitle;
            message.Content = sContent;
            message.TypeId = sTypeId;
            message.PublisherId = sPublisherId;
            message.PublishDate = sPublishDate;
            message.FinishDate = sFinishDate;
            message.RangeType = sRangeType;
            message.RangeId = sRangeId;
            message.Status = sStatus;
            message.IsMsgRemind = bIsMsgRemind;
            message.IsPhoneRemind = bIsPhoneRemind;
            string sResult = oCollaborate.SaveCollaborativeTopic(message);
            if (sResult != "")
            {
                hdId.Value = sResult;
                attUpload.SaveAttaches("0040", sResult);
                Response.Write("<script language='javascript' type='text/javascript'>alert('保存成功！');</script>");
                trRemind.Visible = false;
                attUpload.EditShow("0040", sResult);
            }
        }
    }

    public class MyCustomCompare : IEqualityComparer<MsCollaborativeType>
    {
        #region IEqualityComparer<MsCollaborativeType> 成员

        public bool Equals(MsCollaborativeType x, MsCollaborativeType y)
        {
            if (x.TypeId == y.TypeId)
                return true;
            return false;
        }

        public int GetHashCode(MsCollaborativeType obj)
        {
            return obj.ToString().GetHashCode();
        }

        #endregion
    }
}
